package swing.frame;

import api.BoardServer;
import client.UserSession;
import swing.BaseFrame;
import swing.frame.whiteboard.MyDrawPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class ChatFrame extends BaseFrame {

    //窗口组件容器
    public JPanel contentPanel;

    private JScrollPane msgScroll;
    private JScrollPane msgEditScroll;
    private JButton msgSendButton;

    private JTextArea msgListArea;
    private JTextArea  msgEditField;
    private String msgContent="";
   private MyDrawPanel myDrawPanel;
   public ChatFrame(MyDrawPanel myDrawPanel){
       this.myDrawPanel=myDrawPanel;
   }

    @Override
    protected void initFrame() {
        this.setTitle("聊天室");
        contentPanel = new JPanel();
        contentPanel.setLayout(null);
        Color color = new Color(230, 240, 255);
        contentPanel.setBackground(color);
        this.setContentPane(contentPanel);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setPreferredSize(new Dimension(300, 800));
        this.setResizable(false);
        this.pack();
    }

    @Override
    protected void initAllComponent() {
        //群聊信息列表
        msgListArea = new JTextArea();
        msgListArea.setEditable(false);
        msgListArea.setBorder(null);
        msgListArea.setLineWrap(true);
        msgListArea.setWrapStyleWord(true);
        msgScroll = new JScrollPane(msgListArea);
        msgScroll.setOpaque(false); //设置背景透明
        msgScroll.setBounds(10, 10, 275, 670);
        msgScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        //消息编辑框
        msgEditField = new JTextArea();
        msgEditField.setLineWrap(true);
        msgEditField.setBorder(null);
        msgEditField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==KeyEvent.VK_ENTER){
                    e.consume();//屏蔽换行
                    send();
                }
            }
        });
        msgEditScroll = new JScrollPane(msgEditField);
        msgEditScroll.setOpaque(false); //设置背景透明
        msgEditScroll.setBounds(10, 687, 200, 70);

        //消息发送按钮
        msgSendButton = new JButton("发送");
        msgSendButton.setBounds(215, 688, 70, 63);
        msgSendButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                send();

            }
        });

    }
    public void send(){
        String msg = msgEditField.getText().trim();
        msgEditField.setText("");
        if (msg.isEmpty()) {
            showMessageDialog("发送消息不能为空");
            return;
        }
        String sendMsg = UserSession.UserAccount() + ": " + msg + "\n";
//                String sendMsg = "「周杰伦」" + ": " + msg + "\n";
        flushMessage(sendMsg);
        BoardServer.boardService.broadcastMsg(myDrawPanel.whiteboard.getId(), sendMsg);
    }

    @Override
    protected void addAllComponent() {
        contentPanel.add(msgScroll);
        contentPanel.add(msgEditScroll);
        contentPanel.add(msgSendButton);
    }

    public void flushMessage(String msg) {
        msgContent = msgContent + msg;
        msgListArea.setText(msgContent);
    }

}
